import { ElMessage } from "element-plus";

export default {
  resultDownload(response) {
    const content = response.data;
    const headers = response.headers;

    // 从响应头中提取文件名（处理中文需解码）
    const fileName = headers["content-disposition"]?.match(
      /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
    )?.[1];
    const finalFileName = decodeURIComponent(fileName);

    // 创建Blob对象
    const blob = new Blob([content], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });

    // 生成下载链接
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    link.download = finalFileName;
    link.click();
    URL.revokeObjectURL(link.href);
    ElMessage.success("导出成功！");
  },
};
